package main

import (
	"sync"

	reflectPb "gitee.com/wu-jin-feng/fgin/demos/fconsul_test/client/pb"
	"github.com/gin-gonic/gin"

	"gitee.com/wu-jin-feng/fgin"
)

func main() {
	// fgin.ReginsterGrpcDialOptions([]grpc.DialOption{
	// 	grpc.WithTransportCredentials(insecure.NewCredentials()),
	// }, true, true)
	f := fgin.Fgin{}
	r := f.GetHttpServer("./test.yaml")
	log := f.GetFginLogger()
	// grpcConn, err := fgin.GetGrpcClientConnV2("test_grpc", "test_grpc", "")
	grpcConn, err := f.GetFgrpcClientConn("test_grpc")
	if err != nil {
		panic(err)
	}
	c := reflectPb.NewDemoServiceClient(grpcConn)
	r.GET("/test", func(ctx *gin.Context) {
		log.Info("哈哈哈哈哈")
		res, err := c.GetScheme(ctx, &reflectPb.GetSchemeReq{
			Path: "321222",
			Info: &reflectPb.GetSchemeRespInfo{
				Info: "哈哈哈哈",
				Name: "gogo",
			},
		})
		if err != nil {
			fgin.ReturnJson(ctx, 500, "grpc fail", err)
			return
		}
		fgin.ReturnJson(ctx, 200, res, nil)
	})

	r.GET("/test1", func(ctx *gin.Context) {
		var wg sync.WaitGroup
		for i := 0; i < 100000; i++ {
			wg.Add(1)
			func() {
				defer wg.Done()
				res, err := c.GetScheme(ctx, &reflectPb.GetSchemeReq{
					Path: "321222",
					Info: &reflectPb.GetSchemeRespInfo{
						Info: "哈哈哈哈",
						Name: "gogo",
					},
				})
				if err != nil {
					fgin.ReturnJson(ctx, 500, "grpc fail", err)
					return
				}
				fgin.ReturnJson(ctx, 200, res, nil)

			}()
		}
		wg.Wait()
	})

	f.Run(r)
}
